package com.member.model;

import javax.servlet.*;
import javax.servlet.http.*;
import org.json.JSONArray;
import org.json.JSONException;

import com.carorderdetail.model.CarOrderDetailDAO;
import com.carorderdetail.model.CarOrderDetailJsonHandler;
import com.carorderdetail.model.CarOrderDetailVO;
import com.member.model.MemberJsonHandler;
import com.member.model.MemberVO;
import com.rider.model.CarRiderPathDAO;
import com.rider.model.CarRiderPathJsonHandler;
import com.rider.model.CarRiderPathVO;

import java.io.*;
import java.util.List;

public class MemberServletAndroid extends HttpServlet {
    public void doGet(HttpServletRequest rq, HttpServletResponse rp) 
    				throws ServletException, IOException{
    	MemberDAO memberDAO = new MemberDAO();

    	CarOrderDetailDAO  carorderdetailDAO=new CarOrderDetailDAO();
    	CarRiderPathDAO carriderpathDAO=new  CarRiderPathDAO();
    	List<CarOrderDetailVO> orderdetaillist;
    	String member_id=null;
    	String rider_path_id=null;
		String account = rq.getParameter("account");
		String password = rq.getParameter("password");
		//String memberJsonStr=null;
		//String carorderoailStr=null;
		String CarRiderPathJsonStr="";
		if (account==null){
			account="";					
		}
		if (password==null){
			password="";					
		}
    	boolean isMemberValid = memberDAO.isMemberValid(account, password);
    	
    	if (isMemberValid){
    				

    		member_id=memberDAO.getMemIdByAcc(account);  

    		orderdetaillist=carorderdetailDAO.getRider_path_id(member_id);    

    		List<CarRiderPathVO> carriderpathlist = carriderpathDAO.findByPrimaryKeyand(orderdetaillist);
    		CarRiderPathJsonHandler carriderPathJsonHandler =new CarRiderPathJsonHandler();
    		CarRiderPathJsonStr=carriderPathJsonHandler.toJSONArray(carriderpathlist).toString();

  
    		/*
    		     		List<CarOrderDetailVO> carorderdetaillist=carorderdetailDAO.findorder(member_id);       	
    		List<CarRiderPathVO> carriderpath =carriderpathDAO.getAll();
    		CarOrderDetailJsonHandler carorderoailjsonhandler=new CarOrderDetailJsonHandler();
    		carorderoailStr=carorderoailjsonhandler.toJSONArray(carorderdetaillist).toString();
    	
    		MemberJsonHandler memberJsonHandler = new MemberJsonHandler();
    		memberJsonStr = memberJsonHandler.toJSONArray(member).toString();
	*/
    	}
		
        rp.setContentType("text/html; charset=utf-8");
        PrintWriter out = rp.getWriter();
        out.println(CarRiderPathJsonStr); 
        out.close();
  
    }
    
    public void doPost(HttpServletRequest rq, HttpServletResponse rp) 
    				throws ServletException, IOException{
    	MemberDAO memberDAO = new MemberDAO();

    	CarOrderDetailDAO  carorderdetailDAO=new CarOrderDetailDAO();
    	CarRiderPathDAO carriderpathDAO=new  CarRiderPathDAO();
    	List<CarOrderDetailVO> orderdetaillist;
    	String member_id=null;
    	String rider_path_id=null;
		String account = rq.getParameter("account");
		String password = rq.getParameter("password");
		//String memberJsonStr=null;
		//String carorderoailStr=null;
		String CarRiderPathJsonStr="";
		if (account==null){
			account="";					
		}
		if (password==null){
			password="";					
		}
    	boolean isMemberValid = memberDAO.isMemberValid(account, password);
    	
    	if (isMemberValid){
    				

    		member_id=memberDAO.getMemIdByAcc(account);  

    		orderdetaillist=carorderdetailDAO.getRider_path_id(member_id);    

    		List<CarRiderPathVO> carriderpathlist = carriderpathDAO.findByPrimaryKeyand(orderdetaillist);
    		CarRiderPathJsonHandler carriderPathJsonHandler =new CarRiderPathJsonHandler();
    		CarRiderPathJsonStr=carriderPathJsonHandler.toJSONArray(carriderpathlist).toString();

  
    		/*
    		     		List<CarOrderDetailVO> carorderdetaillist=carorderdetailDAO.findorder(member_id);       	
    		List<CarRiderPathVO> carriderpath =carriderpathDAO.getAll();
    		CarOrderDetailJsonHandler carorderoailjsonhandler=new CarOrderDetailJsonHandler();
    		carorderoailStr=carorderoailjsonhandler.toJSONArray(carorderdetaillist).toString();
    	
    		MemberJsonHandler memberJsonHandler = new MemberJsonHandler();
    		memberJsonStr = memberJsonHandler.toJSONArray(member).toString();
	*/
    	}
		
        rp.setContentType("text/html; charset=utf-8");
        PrintWriter out = rp.getWriter();
        out.println(CarRiderPathJsonStr); 
        out.close();
    }
}